package com.king.zxing.app;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.king.zxing.app.dao.UserDao;
import com.king.zxing.app.domain.User;

import java.util.Objects;

public class LoginActivity extends AppCompatActivity {
    private ActivityResultLauncher<Intent> registerLauncher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        //拿到输入的用户名
        EditText usernameEditText = (EditText) findViewById(R.id.usernameEditText);
        //拿到输入的密码
        EditText passwordEditText = (EditText) findViewById(R.id.passwordEditText);
        //同意《服务协议》和《隐私政策》
        ImageView confirmXieYi = (ImageView) findViewById(R.id.confirm_protocol);
        //false:没勾选
        final boolean[] isChecked = {false};
        //点击勾选同意协议按钮
        confirmXieYi.setOnClickListener(v -> {
            isChecked[0] = !isChecked[0];
            //如果这个勾选了
            if (isChecked[0]) {
                confirmXieYi.setImageResource(R.drawable.check);
            } else {
                confirmXieYi.setImageResource(R.drawable.unchecked);
            }
        });
        //这里开始写代码逻辑
        Button loginBtn = (Button) findViewById(R.id.loginButton);
        loginBtn.setOnClickListener(v -> {

            //转成字符串
            String username = usernameEditText.getText().toString();
            String password = passwordEditText.getText().toString();
            TextView usernamePrompt = (TextView) findViewById(R.id.username_prompt_text);
            TextView passwordPrompt = (TextView) findViewById(R.id.password_prompt_text);
            TextView agreeProtocolPrompt = (TextView) findViewById(R.id.agree_protocol);

            usernamePrompt.setVisibility(username.isEmpty() ? View.VISIBLE : View.INVISIBLE);
            passwordPrompt.setVisibility(password.isEmpty() ? View.VISIBLE : View.INVISIBLE);
            agreeProtocolPrompt.setVisibility(!isChecked[0] ? View.VISIBLE : View.INVISIBLE);

            //用户名不为空、密码不为空、协议被同意
            if (!username.isEmpty() && !password.isEmpty() && isChecked[0]) {
                usernamePrompt.setVisibility(View.INVISIBLE);
                passwordPrompt.setVisibility(View.INVISIBLE);
                agreeProtocolPrompt.setVisibility(View.INVISIBLE);
                //创建一个User对象
                User user = (new UserDao(LoginActivity.this)).queryUserByUsernameAndPassword(username, password);
                if (user != null) {
                    //把User对象传给MainActivity
                    Intent intent = new Intent(LoginActivity.this, TabActivity.class);
                    intent.putExtra("user", user);
                    startActivity(intent);
                }else{
                    showMsg("账号密码错误，登录失败！");
                }
            }

        });
        // 注册 ActivityResultLauncher
        registerLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result.getResultCode() == RESULT_OK) {
                Intent data = result.getData();
                if (data != null) {
                    User user = (User) Objects.requireNonNull(data.getExtras()).get("user");
                    // 在 LoginActivity 的 EditText 组件中显示用户名和密码
                    assert user != null;
                    usernameEditText.setText(user.username);
                    passwordEditText.setText(user.userPassword);
                }
            }
        });

        TextView registerBtn = findViewById(R.id.registerButton);
        registerBtn.setOnClickListener(v -> {
            //从当前的这个界面跳转到注册界面去
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            //启动intent，执行界面跳转
            registerLauncher.launch(intent);
        });
        //点击忘记密码按钮
        TextView forgetPassBtn = findViewById(R.id.forgetPassButton);
        forgetPassBtn.setOnClickListener(v->{
            //从当前的这个界面跳转到忘记密码界面去
            Intent intent = new Intent(LoginActivity.this, ForgetPasswordActivity.class);
            //启动intent，执行界面跳转
            registerLauncher.launch(intent);
        });


    }

    private void showMsg(CharSequence llw) {
        Toast.makeText(this, llw, Toast.LENGTH_SHORT).show();
    }
}

